package project.active;

import java.util.ArrayList;

import project.dao.TestProjectDAO;
import project.model.PrProject;

import model.pojo.Employee;
import model.pojo.Languages;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import approval.dao.LangDAO;
import approve.action.R;

public class AddErrorManager extends Activity{
	private String lang="EN",database="admin";
	private boolean read = true;
	private Bundle alllangs = new Bundle();
	private EditText txtContent;
	private TextView txtTile;
	private Button button;
	private Intent intent;
	private String parseTitle,parseContent;
	private String namecolor="#B023AB";
	RadioButton rdnofix,rderror,rdfixcomplete;

	Bundle language;

	TextView logout,username;
	Employee emp=new Employee();

	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}

	public String getLang(){
		Bundle extras = getIntent().getExtras();
		if(extras != null){
			lang = extras.getString("lang");
		}
		return lang;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.adderror);

		intent = this.getIntent();
		emp = (Employee)intent.getSerializableExtra("employee");

		language = getIntent().getExtras().getBundle(getLang());

		txtTile = (TextView)findViewById(R.id.txtTitleError);
		txtContent = (EditText)findViewById(R.id.txtContent);
		parseTitle = txtTile.getText().toString();
		parseContent = txtContent.getText().toString();
		button = (Button)findViewById(R.id.bttAddError);


		logout=(TextView)findViewById(R.id.logout);
		username=(TextView)findViewById(R.id.username);

		logout.setOnClickListener(listenerLogout);
		username.setText(emp.getUserName());

		rdnofix = (RadioButton)findViewById(R.id.addnofix);
		rderror = (RadioButton)findViewById(R.id.adderror);
		rdfixcomplete = (RadioButton)findViewById(R.id.addfixcomplete);

		rdnofix.setText(language.getString("status_error2"));
		rderror.setText(language.getString("error"));
		rdfixcomplete.setText(language.getString("statusfixcopmp"));
		rdnofix.setOnClickListener(colorNoFix);
		rderror.setOnClickListener(colorError);
		rdfixcomplete.setOnClickListener(colorFixComplete);
		button.setOnClickListener(processAddErrorManager);

		initAttribute();
	}
	private OnClickListener listenerLogout=new OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(AddErrorManager.this, LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
	private OnClickListener colorFixComplete=new OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			namecolor="#3DA850";
			Spannable WordtoSpan = new SpannableString(txtTile.getText().toString());        
			WordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor(namecolor)), 0, txtTile.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			txtTile.setText(WordtoSpan);
		}
	};
	private OnClickListener colorError=new OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			namecolor="#B02323";
			Spannable WordtoSpan = new SpannableString(txtTile.getText().toString());        
			WordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor(namecolor)), 0, txtTile.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			txtTile.setText(WordtoSpan);
		}
	};
	private OnClickListener colorNoFix=new OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			namecolor="#B023AB";
			Spannable WordtoSpan = new SpannableString(txtTile.getText().toString());        
			WordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor(namecolor)), 0, txtTile.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			txtTile.setText(WordtoSpan);
		}
	};

	private OnClickListener processAddErrorManager = new OnClickListener() {

		public void onClick(View v) {

			if(txtTile == null || txtTile.equals("")){
				return ;
			}else{
				//				emp = (Employee)intent.getSerializableExtra("employee");
				PrProject prProject = (PrProject)intent.getSerializableExtra("prprojectError");
				TestProjectDAO.insertError(prProject.getIdProject(),emp.getIdemployee(),txtTile.getText().toString(),txtContent.getText().toString(),getDatabase(),namecolor);
				Intent i = new Intent(AddErrorManager.this, ListErrorProjectManager.class);
				i.putExtras(getIntent().getExtras());
				i.putExtra("prproject", prProject);
				i.putExtra("empl", emp);
				i.putExtra("database", getDatabase());
				i.putExtra("lang", lang);

				startActivity(i);

			}
		}
	};

	public Bundle getAlllangs() {
		if (isRead()) {
			LangDAO lang = new LangDAO();
			Bundle resultVN = new Bundle();
			Bundle resultEN = new Bundle();
			Bundle resultKR = new Bundle();
			try {
				ArrayList<Languages> arr = lang.allLang();
				for (int i = 0; i < arr.size(); i++) {
					resultVN.putString(arr.get(i).getId(), arr.get(i).getVn());
					resultEN.putString(arr.get(i).getId(), arr.get(i).getEn());
					resultKR.putString(arr.get(i).getId(), arr.get(i).getKr());
				};

			} catch (Exception E) {
				E.printStackTrace();
			}
			alllangs.putBundle("VN", resultVN);
			alllangs.putBundle("EN", resultEN);
			alllangs.putBundle("KR", resultKR);
		}
		setRead(false);
		return alllangs;
	}
	public boolean isRead() {
		return read;
	}
	public void setRead(boolean read) {
		this.read = read;
	}

	public void initAttribute(){
		Bundle bundle = this.getAlllangs().getBundle(lang);
		TextView titleErr = (TextView)findViewById(R.id.idAddError);
		titleErr.setText(language.getString("add_error"));

		TextView title = (TextView)findViewById(R.id.idTitle);
		title.setText(bundle.getString("title"));

		TextView content = (TextView)findViewById(R.id.idContent);
		content.setText(language.getString("content"));

		Button btt1 = (Button)findViewById(R.id.bttAddError);
		btt1.setText(language.getString("add_error"));
	}

}
